package cn.ly.core.thread;

public class MyThread extends Thread{
	
	private Thread t;
	private int tnum;
	
	public MyThread(Thread t,int tnum) {
		this.t = t;
		this.tnum = tnum;
	}
	
	public void run(){
		try {
			/**
			 * 在哪个线程内部调用了t.join();
			 * 这个线程被排在 t 线程的后面执行，
			 * 即必须等到t线程执行结束之后，
			 * 当前线程才能从阻塞状态出来，
			 * 进入可运行状态
			 */
			this.t.join();
			boolean flag = true;
			int i = 0;
			while(flag){
				System.out.println("this is MyThread t" + tnum + " " + (++i));
				this.sleep(1000);
				if(i==3){
					flag = false;
				}
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public static void main(String[] args) {
		Thread t1 = new Thread(()->{
			try {			
				boolean flag = true;
				int i = 0;
				while(flag){
					System.out.println("this is Thread t1: " + (++i));
					Thread.sleep(1000);
					if(i==5){
						flag = false;
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});

		Thread tt = new Thread(()->{
			try {
				boolean flag = true;
				int i = 0;
				while(flag){
					System.out.println("this is Thread tt: " + (++i));
					Thread.sleep(2000);
					if(i==5){
						flag = false;
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});

		//Thread t2 = new MyThread(t1,2);//join方法可以控制线程执行顺序
		//Thread t3 = new MyThread(t2,3);
		/*Thread t4 = new MyThread(t1,4);
		Thread t5 = new MyThread(t1,5);*/
		
		try {
			/*t5.start();
			t4.start();
			t3.start();
			t2.start();*/
			t1.start();
			tt.start();
			t1.join();
			tt.join();
			System.out.println("================");
			//t1.sleep(1000*3);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
